package com.chensq.devops


def scan(String projectVersion="", Boolean waitScan = true) {
    this.waitScan = waitScan
    this.msg = new BuildMessage()
    if (projectVersion == ""){
        projectVersion = sh(returnStdout: true, script: 'git log --oneline -n 1|cut -d " " -f 1')
    }
    sh "echo '\nsonar.projectVersion=${projectVersion}' >> sonar-project.properties"
    sh "cat sonar-project.properties"
    return this
}

def start() {
    try {
        this.startToSonar()
    }
    catch (Exception exc) {
        throw exc
    }
    return this
}


def startToSonar() {
    sh """
        sonar-scanner \
        -Dsonar.host.url=http://192.168.31.204:30003 \
        -Dsonar.login=02681acc99665fefb8694ed1f4dc91cc5afc80ce \
        -Dsonar.projectKey="${env.JOB_NAME}" \
        -Dsonar.projectName="${env.JOB_NAME}" \
        -Dsonar.sources=. \
        -Dsonar.sourceEncoding=UTF-8
    """

    if (this.waitScan) {
        // 等待 3 分钟
        timeout(time: 3, unit: 'MINUTES') {
            // 获取质量门限结果
            def qualityGateStatus = sh(script: """
                curl -s -u 02681acc99665fefb8694ed1f4dc91cc5afc80ce: http://192.168.31.204:30003/api/qualitygates/project_status?projectKey=${env.JOB_NAME} | jq -r .projectStatus.status
            """, returnStdout: true).trim()

            String stage = "${env.stage_name}"
            if (qualityGateStatus != 'OK') {
                this.msg.updateBuildMessage(env.BUILD_TASKS, "${stage} Failed...  ×")
                updateGitlabCommitStatus(name: "${stage}", state: 'failed')
                error "Pipeline aborted due to quality gate failure: ${qualityGateStatus}"
            } else {
                this.msg.updateBuildMessage(env.BUILD_RESULT, "${stage} OK...  \u221A")
                updateGitlabCommitStatus(name: "${stage}", state: 'success')
            }
        }
    } else {
        echo "skip waitScan"
    }

    return this
}



// def startToSonar() {
//     sh """
        
//         sonar-scanner \
//         -Dsonar.host.url=http://192.168.31.204:30003 \
//         -Dsonar.login=02681acc99665fefb8694ed1f4dc91cc5afc80ce \
//         -Dsonar.projectKey="${env.JOB_NAME}" \
//         -Dsonar.projectName="${env.JOB_NAME}" \
//         -Dsonar.sources=. \
//         -Dsonar.sourceEncoding=UTF-8
//     """

//     if (this.waitScan) {
//         // 等待 3 分钟
//         timeout(time: 3, unit: 'MINUTES') {
//             def qg = waitForQualityGate()
//             String stage = "${env.stage_name}"
//             if (qg.status != 'OK') {
//                 this.msg.updateBuildMessage(env.BUILD_TASKS, "${stage} Failed...  ×")
//                 updateGitlabCommitStatus(name: "${stage}", state: 'failed')
//                 error "Pipeline aborted due to quality gate failure: ${qg.status}"
//             } else {
//                 this.msg.updateBuildMessage(env.BUILD_RESULT, "${stage} OK...  √")
//                 updateGitlabCommitStatus(name: "${stage}", state: 'success')
//             }
//         }
//     } else {
//         echo "skip waitScan"
//     }

//     return this
// }

// def startToSonar() {
//     withSonarQubeEnv('sonarqube') {
//         sh "第一"
//         sh "sonar-scanner -X;"
//         sh "第二"
//         sleep 5
//     }
//     if(this.waitScan){
//         //wait 3min
//         timeout(time: 3, unit: 'MINUTES') {
//             def qg = waitForQualityGate()
//             String stage = "${env.stage_name}"
//             if (qg.status != 'OK') {
//                 this.msg.updateBuildMessage(env.BUILD_TASKS, "${stage} Failed...  ×")
//                 updateGitlabCommitStatus(name: "${stage}", state: 'failed')
//                 error "Pipeline aborted due to quality gate failure: ${qg.status}"
//             }else{
//                 this.msg.updateBuildMessage(env.BUILD_RESULT, "${stage} OK...  √")
//                 updateGitlabCommitStatus(name: "${stage}", state: 'success')
//             }
//         }
//     }else{
//         echo "skip waitScan"
//     }
//     return this
// }